import 'package:flutter/cupertino.dart';
import 'package:flutter/gestures.dart';
import 'empty_widget.dart';
import '../loadding_or_toast//loadding.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';

//上拉刷新下拉加载的控件
Widget refreshList({
  Key? key,

  /// 控制器
  EasyRefreshController? controller,

  /// 刷新回调(null为不开启刷新)
  OnRefreshCallback? onRefresh,

  /// 加载回调(null为不开启加载)
  OnLoadCallback? onLoad,

  /// 是否开启控制结束刷新
  bool enableControlFinishRefresh = false,

  /// 是否开启控制结束加载
  bool enableControlFinishLoad = false,

  /// 任务独立(刷新和加载状态独立)
  bool taskIndependence = false,

  /// Header
  Header? header,
  required int headerIndex,

  /// Footer
  Footer? footer,

  /// 子组件
  required Widget child,

  /// 首次刷新
  bool firstRefresh = false,

  /// 首次刷新组件
  /// 不设置时使用header
  Widget? firstRefreshWidget,

  /// 空视图
  /// 当不为null时,只会显示空视图
  /// 保留[headerIndex]以上的内容
  ///滑动控制
  Widget? emptyWidget,

  /// 顶部回弹(Header的overScroll属性优先，且onRefresh和header都为null时生效)
  bool topBouncing = true,

  /// 底部回弹(Footer的overScroll属性优先，且onLoad和footer都为null时生效)
  bool bottomBouncing = true,

  /// 滚动行为
  ScrollBehavior? behavior,
  ScrollController? scrollController,
}) {
  var firstWidget = firstRefreshWidget == null ? firstRefreshWidget : ProgressDialog.showFirstLoading();
  var headerWidget = header == null ? ClassicalHeader() : header;
  var footerWidget = footer == null ? ClassicalFooter() : footer;

  var emptyPageWidget = emptyWidget == null ? EmptyWidget() : emptyWidget;

  return EasyRefresh(
    key: key,
    controller: controller,
    child: child,
    onRefresh: onRefresh,
    onLoad: onLoad,
    enableControlFinishRefresh: enableControlFinishRefresh,
    enableControlFinishLoad: enableControlFinishLoad,
    taskIndependence: taskIndependence,
    scrollController: scrollController,
    header: headerWidget,
    footer: footerWidget,
    firstRefresh: firstRefresh,
    firstRefreshWidget: firstWidget,
    headerIndex: headerIndex,
    emptyWidget: firstRefresh?emptyPageWidget:emptyWidget,
    topBouncing: topBouncing,
    bottomBouncing: bottomBouncing,
//    behavior: behavior,
  );
}

/// custom构造器(推荐)
/// 直接使用CustomScrollView可用的slivers
Widget refreshListCustom({
  Key? key,

  /// 控制器
  EasyRefreshController? controller,

  /// CustomListView Key
  Key? listKey,

  /// 刷新回调(null为不开启刷新)
  OnRefreshCallback? onRefresh,

  /// 加载回调(null为不开启加载)
  OnLoadCallback? onLoad,

  /// 是否开启控制结束刷新
  bool enableControlFinishRefresh = false,

  /// 是否开启控制结束加载
  bool enableControlFinishLoad = false,

  /// 任务独立(刷新和加载状态独立)
  bool taskIndependence = false,

  /// Header
  Header? header,
  required int headerIndex,

  /// Footer
  Footer? footer,

  /// 列表方向
  Axis? scrollDirection,

  /// 反向
  bool reverse = false,
  ScrollController? scrollController,
  bool? primary,
  bool shrinkWrap = false,
  Key? center,
  double anchor = 0.0,
  double? cacheExtent,
  int? semanticChildCount,
  DragStartBehavior dragStartBehavior = DragStartBehavior.start,

  /// 首次刷新
  bool firstRefresh = false,

  /// 首次刷新组件
  /// 不设置时使用header
  Widget? firstRefreshWidget,

  /// 空视图
  /// 当不为null时,只会显示空视图
  /// 保留[headerIndex]以上的内容
  Widget? emptyWidget,

  /// 顶部回弹(Header的overScroll属性优先，且onRefresh和header都为null时生效)
  bool topBouncing = true,

  /// 底部回弹(Footer的overScroll属性优先，且onLoad和footer都为null时生效)
  bool bottomBouncing = true,

  /// 滚动行为
  ScrollBehavior? behavior,

  /// Slivers集合
  required List<Widget> slivers,
}) {
  var firstWidget = firstRefreshWidget == null ? firstRefreshWidget : ProgressDialog.showFirstLoading();
  var headerWidget = header == null ? ClassicalHeader() : header;
  var footerWidget = footer == null ? ClassicalFooter() : footer;
  var emptyPageWidget = emptyWidget == null ? EmptyWidget() : emptyWidget;
  return EasyRefresh.custom(
    slivers: slivers,
    key: key,
    listKey: listKey,
    controller: controller,
    onRefresh: onRefresh,
    onLoad: onLoad,
    enableControlFinishRefresh: enableControlFinishRefresh,
    enableControlFinishLoad: enableControlFinishLoad,
    taskIndependence: taskIndependence,
    header: headerWidget,
    headerIndex: headerIndex,
    footer: footerWidget,
    scrollDirection: scrollDirection == null?Axis.vertical:scrollDirection,
    reverse: reverse,
    scrollController: scrollController,
    primary: primary,
    shrinkWrap: shrinkWrap,
    center: center,
    anchor: anchor,
    cacheExtent: cacheExtent,
    semanticChildCount: semanticChildCount,
    dragStartBehavior: dragStartBehavior,
    firstRefresh: firstRefresh,
    firstRefreshWidget: firstWidget,
    emptyWidget: firstRefresh?emptyPageWidget:emptyWidget,
    topBouncing: topBouncing,
    bottomBouncing: bottomBouncing,
//    behavior: behavior,
  );
}

/// 用法灵活,但需将physics、header和footer放入列表中
Widget refreshListBuilder({
  Key? key,
  ScrollController? scrollController,

  /// 控制器
  EasyRefreshController? controller,

  /// 刷新回调(null为不开启刷新)
  OnRefreshCallback? onRefresh,

  /// 加载回调(null为不开启加载)
  OnLoadCallback? onLoad,

  /// 是否开启控制结束刷新
  bool enableControlFinishRefresh = false,

  /// 是否开启控制结束加载
  bool enableControlFinishLoad = false,

  /// 任务独立(刷新和加载状态独立)
  bool taskIndependence = false,

  /// Header
  Header? header,

  /// Footer
  Footer? footer,

  /// 首次刷新
  bool firstRefresh = false,

  /// 顶部回弹(Header的overScroll属性优先，且onRefresh和header都为null时生效)
  bool topBouncing = true,

  /// 底部回弹(Footer的overScroll属性优先，且onLoad和footer都为null时生效)
  bool bottomBouncing = true,

  /// 滚动行为
  ScrollBehavior? behavior,
  required EasyRefreshChildBuilder builder,
}) {
  var headerWidget = header == null ? ClassicalHeader() : header;
  var footerWidget = footer == null ? ClassicalFooter() : footer;
  return EasyRefresh.builder(
    key: key,
    controller: controller,
    onRefresh: onRefresh,
    onLoad: onLoad,
    enableControlFinishRefresh: enableControlFinishRefresh,
    enableControlFinishLoad: enableControlFinishLoad,
    taskIndependence: taskIndependence,
    scrollController: scrollController,
    header: headerWidget,
    footer: footerWidget,
    firstRefresh: firstRefresh,
    topBouncing: topBouncing,
    bottomBouncing: bottomBouncing,
    behavior: behavior,
    builder: builder,
  );
}
